<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\View;
Class Chat extends Index{
	public function __construct(Request $request)
	{
		parent::__construct($request);
	}
	//话题列表
	public function chatlist()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			$page = Db::name('huati')->where(['userid'=>session('userid')])->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
			// dump($page);die;
			$huati = $page->all();
			foreach ($huati as $key => $value) {
				$huati[$key]['nickname'] = Db::name('user')->where(['userid'=>$huati[$key]['userid']])->value('nickname');
			}
			$this->assign('page',$page);
			$this->assign('huati',$huati);
			$this->assign('title','看话题');
			return view('chatlist');
		}
	}
	//话题详情
	public function huatidetail(){
		//话题
		$huatidetail = Db::name('huati')->where(['huatiid'=>input('huatiid')])->find();
		$huatidetail['nickname'] = Db::name('user')->where(['userid'=>$huatidetail['userid']])->value('nickname');
		//评论
		$chatcontent = Db::name('chat')->order('chattime asc')->where(['huatiid'=>$huatidetail['huatiid']])->select();
		foreach ($chatcontent as $key => $value) {
			$chatcontent[$key]['nickname'] = Db::name('user')->where(['userid'=>$chatcontent[$key]['userid']])->value('nickname');
			$chatcontent[$key]['photo'] = Db::name('user')->where(['userid'=>$chatcontent[$key]['userid']])->value('photo');
			if($chatcontent[$key]['huifuid']!=0){
				$chatcontent[$key]['huifunickname'] = Db::name('user')->where(['userid'=>$chatcontent[$key]['huifuuserid']])->value('nickname');
			}else{
				$chatcontent[$key]['huifunickname']='';
			}
		}
		if(empty($chatcontent)){
			$chatcontent = '';
		}

		$this->assign('title',$huatidetail['huatititle']);
		$this->assign('chatcontent',$chatcontent);
		$this->assign('huati',$huatidetail);
		return view('chatdetail');
		// dump($huatidetail);
	}
	// 话题评论
	public function huatichat(){
		$data['huatiid'] = input('post.huatiid');
		$data['chatcontent'] = htmlspecialchars(input('post.chatcontent'));
		$data['userid'] = session('userid');
		$data['chattime'] = time();
		$data['huifuid']=input('post.huifuid');
		$data['huifuuserid']=input('post.huifuuserid');
		$chatcontent = Db::name('chat')->insert($data);
		if($chatcontent){
			jsBack('评论成功','/api/chat/huatidetail?huatiid='.input('post.huatiid'));
		}else{
			jsBack('评论失败');
		}
	}
	/**
	* 创建话题
	*/
	public function chatadd(){
		return view('chatadd');
	}
	public function chatinsert(){	
		$file = request()->file('huatiphoto');
		if($file){
		    // 移动到框架应用根目录/public/uploads/xiangce 目录下
	        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/xiangce');
	        //上传成功后获取传记信息
	        if($info){
	        	$data['huatiphoto']='/public/uploads/xiangce/'.date("Ymd").'/'.$info->getFilename();
		        $data['huaticontent']=input('post.huaticontent');
				$data['huatititle']=input('post.huatititle');
				$data['addtime']=time();
				$data['userid']=session('userid');
				$data['huatitype']='0';
				$huati=Db::name('huati')->insertGetId($data);
				//添加相册
				if($huati){
					jsBack('上传成功','/api/chat/chatlist');
				}else{
					jsBack('上传失败');
					return false;
				}
			}else{
				jsBack('内部错误');
			}
		}else{
			jsBack('请选择图片');
			return false;
		}
	}
}
